<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/4/7
 * Time: 下午3:18
 */

namespace App\Admin\Controllers;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use App\Busi\Traits\VoucherTrait;
use App\Models\Voucher;

use App\Http\Controllers\Controller;

class VoucherController extends Controller
{
    use ModelForm,VoucherTrait;
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('订单券管理');
            $content->description('');
            $content->body($this->grid());
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('新增订单券');
            $content->description('');
            $content->body($this->form());
        });
    }
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑订单券');
            $content->description('');
            $content->body($this->form('edit')->edit($id));
        });
    }

    protected function grid() {
        return Admin::grid(Voucher::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->name('订单券标题');
            $grid->type('类型')->display(function ($type){
                if($type == 1){
                    return "充值";
                }else{
                    return "消费";
                }
            });
            $obj = $this;
            $grid->price("单价");
            $grid->amount("订单券个数");
            $grid->phone("联系电话");
            $grid->remark("备注");

            $grid->flag('是否生效')->display(function($flag){
                if( $flag==1 ) return '有效';
                return '无效';
            });
            $grid->created_at('创建时间');
            $grid->filter(function ($filter) {
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {

            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });
        });
    }

    protected function form($mode='')
    {
        return Admin::form(Voucher::class, function (Form $form) use($mode){
            if($mode=='edit'){
                $form->display('id','ID');
                $form->display('created_at', '创建时间');
                $form->display('updated_at', '更新时间');
            }
            $form->text('name','标题');
            $form->select('type','类型')->options([1=>"系统赠送",2=>"消费"]);
            $form->text('price','单价');
            $form->text('amount','订单券个数');
            $form->text('phone','联系电话');
            $form->text('remark','备注');
            $form->radio('flag', '是否生效')->options([0=>'否', 1=>'是'])->default(1);
        });
    }
}